class AbstractResponse {
  num? code;
  String? message;
  String? timestamp;

  AbstractResponse({
    this.code,
    this.message,
    this.timestamp,
  });
}

class GeneralResponse<T> extends AbstractResponse {
  /// 泛型数据
  T? data;

  GeneralResponse({
    this.data,
    num? code,
    String? message,
    String? timestamp,
  }) {
    this.code = code;
    this.message = message;
    this.timestamp = timestamp;
  }

  /// 业务成功判断
  bool get success => code == 200 || code == 0;

  /// 泛型 fromJson
  factory GeneralResponse.fromJson(
      Map<String, dynamic> json,
      T Function(dynamic json) fromJsonT,
      ) {
    return GeneralResponse<T>(
      code: json['code'],
      message: json['message'],
      timestamp: json['timestamp'],
      data: json['data'] != null ? fromJsonT(json['data']) : null,
    );
  }

  /// toJson
  Map<String, dynamic> toJson(Map<String, dynamic> Function(T value) toJsonT) {
    return {
      'code': code,
      'message': message,
      'timestamp': timestamp,
      'data': data != null ? toJsonT(data as T) : null,
    };
  }
}

/// Pagination
class Pagination {
  int total;
  int pageNum;
  int pageSize;
  int totalPages;
  bool hasNext;
  bool hasPrev;
  int first;
  int last;

  Pagination({
    required this.total,
    required this.pageNum,
    required this.pageSize,
    required this.totalPages,
    required this.hasNext,
    required this.hasPrev,
    required this.first,
    required this.last,
  });

  factory Pagination.fromJson(Map<String, dynamic> json) {
    return Pagination(
      total: json['total'] ?? 0,
      pageNum: json['pageNum'] ?? 1,
      pageSize: json['pageSize'] ?? 20,
      totalPages: json['totalPages'] ?? 0,
      hasNext: json['hasNext'] ?? false,
      hasPrev: json['hasPrev'] ?? false,
      first: json['first'] ?? 1,
      last: json['last'] ?? 1,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'total': total,
      'pageNum': pageNum,
      'pageSize': pageSize,
      'totalPages': totalPages,
      'hasNext': hasNext,
      'hasPrev': hasPrev,
      'first': first,
      'last': last,
    };
  }
}

/// 通用分页响应
class PageableResponse<T> extends AbstractResponse {
  Pagination pagination;
  List<T> data;

  PageableResponse({
    required this.pagination,
    required this.data,
  });

  factory PageableResponse.fromJson(
      Map<String, dynamic> json,
      T Function(dynamic) fromJsonT,
      ) {
    return PageableResponse<T>(
      pagination: Pagination.fromJson(json['pagination'] ?? {}),
      data: (json['data'] as List<dynamic>? ?? [])
          .map((e) => fromJsonT(e))
          .toList(),
    )
      ..code = json['code']
      ..message = json['message']
      ..timestamp = json['timestamp'];
  }

  Map<String, dynamic> toJson(Map<String, dynamic> Function(T) toJsonT) {
    return {
      'code': code,
      'message': message,
      'timestamp': timestamp,
      'pagination': pagination.toJson(),
      'data': data.map((e) => toJsonT(e)).toList(),
    };
  }
}
